package com.ruoyi.warning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.warning.domain.entity.Processor;
import com.ruoyi.warning.mapper.ProcessorsMapper;
import com.ruoyi.warning.service.IProcessorsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenhc
 * @since 2025-03-25
 */
@Service
public class ProcessorsServiceImpl extends ServiceImpl<ProcessorsMapper, Processor> implements IProcessorsService {

    @Override
    public Integer getProcessorById(Integer deviceId) {
        //取平均值
        LambdaQueryWrapper<Processor> wrapper = new QueryWrapper<Processor>().lambda()
                .eq(Processor::getDeviceId, deviceId);
        List<Processor> list = this.list(wrapper);
        int sum = list.stream()
                .mapToInt(Processor::getProcessorUsage)
                .sum();
        if(sum == 0){
            return null;
        }
        //取最大值
//        LambdaQueryWrapper<Processor> wrapper = new QueryWrapper<Processor>().lambda()
//                .eq(Processor::getDeviceId, deviceId)
//                .orderByDesc(Processor::getProcessorUsage)
//                .last("limit 1");
//        Processor processor = this.getOne(wrapper);
//
//        if(ObjectUtils.isNotEmpty(processor)){
//            return processor.getProcessorUsage();
//        }
        return sum/list.size();
    }
}
